package com.ali.resource.ServicePage;

import com.ali.resource.CompoentPage.RedisComponent;
import com.ali.resource.MapPage.rds.RdsMap;
import com.ali.resource.MapPage.redis.RedisMap;
import com.ali.resource.ShareTools.BasicTools;
import com.aliyun.r_kvstore20150101.models.DescribeInstancesRequest;
import com.aliyun.r_kvstore20150101.models.DescribeInstancesResponse;
import com.aliyun.r_kvstore20150101.models.DescribeInstancesResponseBody;
import com.aliyun.rds20140815.models.DescribeDBInstancesRequest;
import com.aliyun.rds20140815.models.DescribeDBInstancesResponse;
import com.aliyun.rds20140815.models.DescribeDBInstancesResponseBody;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class RedisService {

    @Autowired
    private RedisComponent redisComponent;



    public void getXlsx(ArrayList<RedisMap> redisList) throws IOException {
        XSSFWorkbook sheets = new XSSFWorkbook();
        XSSFSheet sheet = sheets.createSheet();
        XSSFRow row = sheet.createRow(0);

        row.createCell(0).setCellValue("instanceId");
        row.createCell(1).setCellValue("instanceName");
        row.createCell(2).setCellValue("instanceStatus");
        row.createCell(3).setCellValue("instanceTagInfo");
        row.createCell(4).setCellValue("instanceSpec");
        row.createCell(5).setCellValue("productType");
        row.createCell(6).setCellValue("maxConnections");
        row.createCell(7).setCellValue("totalCapacity");
        row.createCell(8).setCellValue("iShardCount");
        row.createCell(9).setCellValue("VpcId");
        row.createCell(10).setCellValue("VSwitchId");
        row.createCell(11).setCellValue("environment");
        row.createCell(12).setCellValue("applicationName");
        row.createCell(13).setCellValue("applicationOwner");
        row.createCell(14).setCellValue("techOwner");
        row.createCell(15).setCellValue("vendor");



        int number = 1;
        for (RedisMap redisMap : redisList) {
            XSSFRow row1 = sheet.createRow(number);
            row1.createCell(0).setCellValue(redisMap.getInstanceId());
            row1.createCell(1).setCellValue(redisMap.getInstanceName());
            row1.createCell(2).setCellValue(redisMap.getInstanceStatus());
            row1.createCell(3).setCellValue(redisMap.getInstanceTagInfo());
            row1.createCell(4).setCellValue(redisMap.getInstanceSpec());
            row1.createCell(5).setCellValue(redisMap.getProductType());
            row1.createCell(6).setCellValue(redisMap.getMaxConnections());
            row1.createCell(7).setCellValue(redisMap.getTotalCapacity());
            row1.createCell(8).setCellValue(redisMap.getIShardCount());
            row1.createCell(9).setCellValue(redisMap.getVpcId());
            row1.createCell(10).setCellValue(redisMap.getVSwitchId());
            row1.createCell(11).setCellValue(redisMap.getEnvironment());
            row1.createCell(12).setCellValue(redisMap.getApplicationName());
            row1.createCell(13).setCellValue(redisMap.getApplicationOwner());
            row1.createCell(14).setCellValue(redisMap.getTechOwner());
            row1.createCell(15).setCellValue(redisMap.getVendor());

            number += 1;
        }

        File file = BasicTools.setFIlePath("redis.xlsx");

        FileOutputStream fileOutputStream = new FileOutputStream(file);
        sheets.write(fileOutputStream);
        fileOutputStream.close();
        sheets.close();
    }




    public ArrayList<RedisMap> getRdsList(@RequestParam(required = true,defaultValue = "false") String bool
            , @RequestParam(required = false) String instanceIdPrefix
            , @RequestParam(required = false) String instanceNamePrefix) throws Exception {

        DescribeInstancesResponse instancesResource = redisComponent.getInstancesResource(redisComponent.getRequest());
        int totalRecordCount = instancesResource.getBody().getTotalCount();

        int page = (totalRecordCount / 50);
        if (0 != (totalRecordCount % 50)) {
            page += 1;
        }

        ArrayList<RedisMap> redisList = new ArrayList<>();
        for (int i = 1; i < page + 1; i++) {
            DescribeInstancesRequest redisRequest = redisComponent.getRequest();
            redisRequest.setPageSize(50);
            redisRequest.setPageNumber(i);
            DescribeInstancesResponse redisResource = redisComponent.getInstancesResource(redisRequest);

            List<DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesKVStoreInstance> kvStoreInstance = redisResource.getBody().getInstances().getKVStoreInstance();
            for (DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesKVStoreInstance instance : kvStoreInstance) {

                RedisMap redisMap = this.getRedis(instance);

                if (instanceIdPrefix != null && instanceNamePrefix != null){

                    if (instance.getInstanceId().toLowerCase().contains(instanceIdPrefix.toLowerCase().trim()) && instance.getInstanceName().toLowerCase().contains(instanceNamePrefix.toLowerCase().trim())){
                        redisList.add(redisMap);
                    }

                } else if (instanceIdPrefix != null && instanceNamePrefix == null){

                    if (instance.getInstanceId().toLowerCase().contains(instanceIdPrefix.toLowerCase().trim())){
                        redisList.add(redisMap);
                    }

                } else if (instanceNamePrefix != null && instanceIdPrefix == null){

                    if (instance.getInstanceName().toLowerCase().contains(instanceNamePrefix.toLowerCase().trim())){
                        redisList.add(redisMap);
                    }

                } else if (instanceIdPrefix == null && instanceNamePrefix == null){
                    redisList.add(redisMap);
                }

            }

        }
        return redisList;
    }

    public RedisMap getRedis(DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesKVStoreInstance instance){


        RedisMap redisMap = new RedisMap();

        BigDecimal bigDecimal = new BigDecimal(instance.getCapacity().toString());
        BigDecimal totalMem = bigDecimal.divide(new BigDecimal("1024"));


        redisMap.setInstanceId(instance.getInstanceId());
        redisMap.setInstanceName(instance.getInstanceName());
        redisMap.setInstanceStatus(instance.getInstanceStatus());
        redisMap.setInstanceSpec(instance.getArchitectureType());
        redisMap.setInstanceTagInfo(instance.getInstanceType() +":"+ instance.getEngineVersion());
        redisMap.setProductType(instance.getEditionType());
        redisMap.setMaxConnections(instance.getConnections().toString());
        redisMap.setTotalCapacity(totalMem.toString());
        redisMap.setVpcId(instance.getVpcId());
        redisMap.setVSwitchId(instance.getVSwitchId());
        if (instance.getShardCount() != null){
            redisMap.setIShardCount(instance.getShardCount().toString());
        }

        List<DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesKVStoreInstanceTagsTag> tag = instance.getTags().getTag();
        for (int i = 0; i < tag.size(); i++) {
            if (tag.get(i).getKey().equals("lll:deployment:environment")){
                redisMap.setEnvironment(tag.get(i).getValue());
            }
            if (tag.get(i).getKey().equals("lll:business:application-name")){
                redisMap.setApplicationName(tag.get(i).getValue());
            }
            if (tag.get(i).getKey().equals("lll:business:application-owner")){
                redisMap.setApplicationOwner(tag.get(i).getValue());
            }
            if (tag.get(i).getKey().equals("lll:business:tech-owner")){
                redisMap.setTechOwner(tag.get(i).getValue());
            }
            if (tag.get(i).getKey().equals("lll:business:vendor")){
                redisMap.setVendor(tag.get(i).getValue());
            }
        }

        return redisMap;
    }



}
